﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PotentialRentals.DAL;

namespace PotentialRentals.Models
{
    public class RentalListViewModel
    {
        public IEnumerable<Rental> Rentals { get; set; }
        public string Sort { get; set; }
        public string SortDir { get; set; }
        public RentalFilter Filter { get; set; }

        public RentalListViewModel()
        {
            Filter = new RentalFilter();
            Rentals = new List<Rental>();
        }

        public void ProcessList() {
            FilterRentals();
            SortRentals();
        }

        private void FilterRentals()
        {
            if (Filter.Type)
            {
                Rentals = Rentals.Where(x => x.Type.RentTypeId == Filter.RentTypeId);
            }
            if (Filter.CityState)
            {
                Rentals = Rentals.Where(x => x.Address.State.StateId == Filter.StateId);
                if (Filter.City != null)
                {
                    Rentals = Rentals.Where(x => x.Address.City == Filter.City).AsEnumerable();
                }
            }
            if (Filter.Bedrooms)
            {
                Rentals = Rentals.Where(x => x.Bedrooms >= Filter.BedroomRange.Low && x.Bedrooms <= Filter.BedroomRange.High);
            }
            if (Filter.Bathrooms)
            {
                Rentals = Rentals.Where(x => x.Bathrooms >= Filter.BathroomRange.Low && x.Bathrooms <= Filter.BathroomRange.High);
            }
            if (Filter.Rent)
            {
                Rentals = Rentals.Where(x => x.Rent >= Filter.RentRange.Low && x.Rent <= Filter.RentRange.High);
            }
        }

        private void SortRentals()
        {
            var sortMethod = Sort + SortDir;
            switch (sortMethod)
            {
                case "RentASC":
                    Rentals = Rentals.OrderBy(x => x.Rent).ToList();
                    break;
                case "RentDESC":
                    Rentals = Rentals.OrderByDescending(x => x.Rent).ToList();
                    break;
                case "BedroomsASC":
                    Rentals = Rentals.OrderBy(x => x.Bedrooms).ToList();
                    break;
                case "BedroomsDESC":
                    Rentals = Rentals.OrderByDescending(x => x.Bedrooms).ToList();
                    break;
                case "BathroomsASC":
                    Rentals = Rentals.OrderBy(x => x.Bathrooms).ToList();
                    break;
                case "BathroomsDESC":
                    Rentals = Rentals.OrderByDescending(x => x.Bathrooms).ToList();
                    break;
                case "TypeASC":
                    Rentals = Rentals.OrderBy(x => x.Type).ToList();
                    break;
                case "TypeDESC":
                    Rentals = Rentals.OrderByDescending(x => x.Type).ToList();
                    break;
            }
        }
    }
}